package com.example.check_oa.model.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.check_oa.model.mapper.*;
import com.example.check_oa.model.service.AttendanceService;
import com.example.check_oa.model.service.BusinessTripApService;
import com.example.check_oa.utils.GetAntten;
import com.example.comment_oa.pojos.*;
import com.example.comment_oa.dubbo.SysEmployeeServiceDubbo;
import com.example.comment_oa.pojos.attendance.*;
import com.example.comment_oa.pojos_vo.attendanceVo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author cjl
* @description 针对表【attendance】的数据库操作Service实现
* @createDate 2022-10-26 00:49:30
*/
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
    implements AttendanceService{
    @Resource
    AttendanceMapper attendanceMapper;
    @Resource
    OvertimeApMapper overtimeApMapper;
    @Resource
    HolidayApMapper holidayApMapper;
    @Resource
    BusinessTripApMapper businessTripApMapper;
    @Resource
    LateMapper lateMapper;
    @Resource
    EarlyMapper earlyMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    BusinessTripApService businessTripApService;
    @DubboReference(version = "1.0.0")
    SysEmployeeServiceDubbo sysEmployeeServiceDubbo;
    @Resource
    ReplacementCardApMapper replacementCardApMapper;

    //图标
    @Override
    public List<IconVo> iconList(String day, String userNo,int type) {
        List<IconVo> iconVoList=new ArrayList<>();
        //加班集合
        QueryWrapper<OvertimeAp> ovWrapper=new QueryWrapper<>();
        ovWrapper.eq("user_ot",userNo);
        ovWrapper.eq("ot_state",1);
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(ovWrapper);
        List<String> ii=new ArrayList<>();
        overtimeAps.forEach(e-> {
            List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()), DateUtil.formatDate(e.getOtEndTime()));
            betweenDate.forEach(e1->{
                if(day.substring(0,7).equals(e1.substring(0,7))){
                    ii.add(e1);
                    IconVo iconVo=new IconVo();
                    iconVo.setAge(9);
                    iconVo.setDay(e1);
                    iconVoList.add(iconVo);
                }else{
                    ii.remove(e1);
                }

            });
        });
        //正常上班的
        QueryWrapper<Attendance> wrapper=new QueryWrapper<>();
        wrapper.like("at_time",day.substring(0,7));
        wrapper.eq("user_at",userNo);
        wrapper.eq("at_star_res","正常");
        wrapper.eq("at_end_res","正常");
        if(type==1){
            wrapper.lt("at_time",day);
        }
        List<Attendance> list = attendanceMapper.selectList(wrapper);
        list.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(1);
            iconVo.setDay(DateUtil.formatDate(e.getAtTime()));
            iconVoList.add(iconVo);
        });
        //迟到的
        QueryWrapper<Attendance> wrapper3=new QueryWrapper<>();
        wrapper3.eq("user_at",userNo);
        wrapper3.like("at_time",day.substring(0,7));
        wrapper3.like("at_star_res","迟到");
        if(type==1){
            wrapper3.lt("at_time",day);
        }
        List<Attendance> list3 = attendanceMapper.selectList(wrapper3);
        list3.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(2);
            iconVo.setDay(DateUtil.formatDate(e.getAtTime()));
            iconVoList.add(iconVo);
        });

        //早退的
        QueryWrapper<Attendance> wrapper4=new QueryWrapper<>();
        wrapper4.eq("user_at",userNo);
        wrapper4.like("at_time",day.substring(0,7));
        wrapper4.like("at_end_res","早退");
        if(type==1){
            wrapper4.lt("at_time",day);
        }
        List<Attendance> list4 = attendanceMapper.selectList(wrapper4);
        list4.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(3);
            iconVo.setDay(DateUtil.formatDate(e.getAtTime()));
            iconVoList.add(iconVo);
        });

        //调休集合
        List<String> stringList = GetAntten.get(day.substring(0, 7));
        stringList.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(10);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });

        //旷工
        QueryWrapper<Attendance> wrapper7=new QueryWrapper<>();
        wrapper7.like("at_time",day.substring(0,7));
        wrapper7.eq("user_at",userNo);
        List<String> stringList2 =new ArrayList<>();
        //本月工作日+调休
        List<String> stringList1 = GetAntten.queryMonthDay(day, stringList);
        //当天到月初
        List<String> datetoOne=new ArrayList<>();
        if(type==1){
            wrapper7.lt("at_time",day);
            List<DateToOneVo> dateToOneVos = attendanceMapper.DateToOne(day.substring(0,7)+"-01", day);
            dateToOneVos.forEach(e->datetoOne.add(e.getDate()));
            //求本月工作日集合和今天到月初的交集
            stringList2.addAll(GetAntten.intersectList2(stringList1, datetoOne));
        }
        //当月已打卡的
        List<String> isAtten=new ArrayList<>();
        List<Attendance> attendances = attendanceMapper.selectList(wrapper7);
        for (int a = 0; a < attendances.size(); a++) {
            isAtten.add(DateUtil.formatDate(attendances.get(a).getAtTime()));
        }

        //请假集合
        QueryWrapper<HolidayAp> wrapper8=new QueryWrapper<>();
        wrapper8.eq("user_ho",userNo);
        wrapper8.eq("ho_state",1);
        List<HolidayAp> holidayAps1 = holidayApMapper.selectList(wrapper8);
        List<String> betweenDate4=new ArrayList<>();
        holidayAps1.forEach(e->{
            betweenDate4.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()),DateUtil.formatDate(e.getHoApEndTime())));
        });
        List<String> betweenDate1=new ArrayList<>();
        betweenDate4.forEach(e->{
            if(e.indexOf(day.substring(0,7))==0){
                betweenDate1.add(e);
            }
        });
        //本月调休集合
        List<String> txlist = GetAntten.get(day.substring(0,7));
        List<String> strings = GetAntten.queryMonthDay(day.substring(0,10), txlist);
        List<String> stringList4 = GetAntten.intersectList2(strings, betweenDate1);
        stringList4.forEach(e->{
            IconVo iconVo = new IconVo();
            iconVo.setDay(e);
            iconVo.setAge(7);
            iconVoList.add(iconVo);
        });

        //出差
        QueryWrapper<BusinessTripAp> wrapper6=new QueryWrapper<>();
        wrapper6.eq("user_bt",userNo);
        wrapper6.eq("bt_ap_state",1);
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(wrapper6);
        List<String> busiList=new ArrayList<>();
        List<String> betweenDate=new ArrayList<>();
        businessTripAps.forEach(e->{
            betweenDate.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime())));
        });
        betweenDate.forEach(e1->{
            if(e1.indexOf(day.substring(0,7))==0){
                busiList.add(e1);
            }
        });
        List<String> stringList5 = GetAntten.intersectList2(strings, busiList);
        stringList5.forEach(e->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(8);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });

        QueryWrapper<ReplacementCardAp> rcQueryWrapper=new QueryWrapper<>();
        rcQueryWrapper.eq("user_rc",userNo);
        rcQueryWrapper.eq("rc_state",1);
        rcQueryWrapper.like("rc_time",day.substring(0,7));
        List<IconVo> iiList=new ArrayList<>();
        List<ReplacementCardAp> rcList = replacementCardApMapper.selectList(rcQueryWrapper);
        List<String> rcList2=new ArrayList<>();
        rcList.forEach(e->{
            rcList2.add(DateUtil.formatDate(e.getRcTime()));
            System.out.println("补卡："+e);
        });
        //漏签上班的
        QueryWrapper<Attendance> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_at",userNo);
        wrapper1.like("at_time",day.substring(0,7));
        wrapper1.isNull("at_star_res");
        if(type==1){
            wrapper1.lt("at_time",day);
        }
        List<Attendance> list1 = attendanceMapper.selectList(wrapper1);
        List<String> list11=new ArrayList<>();
        list1.forEach(e->{
            list11.add(DateUtil.formatDate(e.getAtTime()));
        });
        List<String> stringList6 = GetAntten.intersectList2(rcList2, list11);
        stringList6.forEach(e->{
            list11.remove(e);
            //上班补卡的
            IconVo iconVo=new IconVo();
            iconVo.setAge(11);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });
        list11.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(5);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });

        //下班漏签
        QueryWrapper<Attendance> wrapper2=new QueryWrapper<>();
        wrapper2.eq("user_at",userNo);
        wrapper2.like("at_time",day.substring(0,7));
        wrapper2.isNull("at_end_res");
        if(type==1){
            wrapper2.lt("at_time",day);
        }
        List<Attendance> list2 = attendanceMapper.selectList(wrapper2);
        List<String> list22=new ArrayList<>();
        list2.forEach(e->{
            list22.add(DateUtil.formatDate(e.getAtTime()));
        });
        List<String> stringList7 = GetAntten.intersectList2(rcList2, list22);
        stringList7.forEach(e->{
            list22.remove(e);
            //下班补卡的
            IconVo iconVo=new IconVo();
            iconVo.setAge(12);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });
        list22.forEach((e)->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(6);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
        });

        Map<Object, Long> collect2 = rcList.stream().collect(
                Collectors.groupingBy( ReplacementCardAp::getRcTime , Collectors.counting()  )   );
        List<Object> collect = collect2.keySet().stream().filter(key -> collect2.get(key) > 1).collect(Collectors.toList());
        //旷工一天补都卡拿出来
        List<String> cllect2=new ArrayList<>();
        collect.forEach(e->{
            cllect2.add(DateUtil.formatDate((Date) e));
        });
        cllect2.forEach(e->{
            IconVo iconVo=new IconVo();
            iconVo.setAge(11);
            iconVo.setDay(e);
            iconVoList.add(iconVo);
            iconVo=new IconVo();
            iconVo.setDay(e);
            iconVo.setAge(12);
            iconVoList.add(iconVo);
        });
        //已打卡and当天到月初的工作日差集，算出旷工
        if(type==1){
            List<String> stringList3 = GetAntten.subList2(stringList2, isAtten);
            stringList3.removeAll(betweenDate1);//去除请假
            stringList3.removeAll(cllect2);//去除补卡
            stringList3.removeAll(ii);//去除加班
            stringList3.removeAll(busiList);//去除出差
            stringList3.forEach(e->{
                IconVo iconVo=new IconVo();
                iconVo.setAge(4);
                iconVo.setDay(e);
                iconVoList.add(iconVo);
            });
        }else{
            List<String> stringList3 = (List<String>) CollectionUtil.subtract(stringList1, isAtten);
            stringList3.removeAll(betweenDate1);//去除请假
            stringList3.removeAll(ii);//去除加班
            stringList3.removeAll(cllect2);//去除补卡
            stringList3.removeAll(busiList);//去除出差
            stringList3.forEach(e->{
                IconVo iconVo=new IconVo();
                iconVo.setAge(4);
                iconVo.setDay(e);
                iconVoList.add(iconVo);
            });
        }

        for (int i = 0; i < iconVoList.size(); i++) {
            if(iconVoList.get(i).getAge()==4){
                for (int i1 = 0; i1 < rcList.size(); i1++) {
                    if(iconVoList.get(i).getDay().equals(DateUtil.formatDate(rcList.get(i1).getRcTime()))){
                        iiList.add(iconVoList.get(i));
                        if(rcList.get(i1).getRcAttendanceTypeId().equals("上班卡")){
                            IconVo iconVo=new IconVo();
                            iconVo.setAge(11);
                            iconVo.setDay(DateUtil.formatDate(rcList.get(i1).getRcTime()));
                            iconVoList.add(iconVo);
                            iconVo=new IconVo();
                            iconVo.setAge(6);
                            iconVo.setDay(DateUtil.formatDate(rcList.get(i1).getRcTime()));
                            iconVoList.add(iconVo);
                        }
                        if(rcList.get(i1).getRcAttendanceTypeId().equals("下班卡")){
                            IconVo iconVo=new IconVo();
                            iconVo.setAge(12);
                            iconVo.setDay(DateUtil.formatDate(rcList.get(i1).getRcTime()));
                            iconVoList.add(iconVo);
                            iconVo=new IconVo();
                            iconVo.setAge(5);
                            iconVo.setDay(DateUtil.formatDate(rcList.get(i1).getRcTime()));
                            iconVoList.add(iconVo);
                        }
                    }
                }
            }
        }
        Collection<IconVo> disjunction = CollUtil.disjunction(iconVoList, iiList);
        return (List<IconVo>) disjunction;
    }

    //打卡
    @Override
    public int add(String day, int type) {
        System.out.println(day);
        System.out.println("几点钟："+day.substring(11,16));
        String user = stringRedisTemplate.opsForValue().get("user");
        JSONObject jsonObject = JSONObject.parseObject(user);
        JSONObject sysEmployee = JSONObject.parseObject(jsonObject.getString("sysEmployee"));
        JSONObject dept = JSONObject.parseObject(sysEmployee.getString("dept"));
        Attendance attendance=new Attendance();
        attendance.setAtTime(DateUtil.parse(day, "yyyy-MM-dd"));
        attendance.setUserAt(sysEmployee.getString("empCard"));
        attendance.setEmpName(sysEmployee.getString("empName"));
        attendance.setDeptName(dept.getString("deptName"));
        boolean flog=true;

        //本月调休集合
        List<String> list = GetAntten.get(day.substring(0,7));
        List<String> strings = GetAntten.queryMonthDay(day.substring(0,10), list);
        List<String> dateList=new ArrayList<>();
        dateList.add(day.substring(0,10));
        if(GetAntten.intersectList2(strings,dateList).size()==0){
            System.out.println("不是出勤日！");
            flog=false;
            return 3;
        }

        //加班
        QueryWrapper<OvertimeAp> ovWrapper=new QueryWrapper<>();
        ovWrapper.eq("user_ot",sysEmployee.getString("empCard"));
        ovWrapper.eq("ot_state",1);
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(ovWrapper);
        //接收日期加班集合
        List<String> betweenDate =new ArrayList<>();
        //添加加班日期
        overtimeAps.forEach(e->{
            betweenDate.addAll(GetAntten.getBetweenDate(DateUtil.formatDateTime(e.getOtStartTime()), DateUtil.formatDateTime(e.getOtEndTime())));
        });
        for (int i = 0; i < betweenDate.size(); i++) {
            if(day.substring(0,10).equals(betweenDate.get(i))){
                flog=false;
                return 4;
            }
        }
        //出差
        QueryWrapper<BusinessTripAp> busiWrapper=new QueryWrapper<>();
        busiWrapper.eq("user_bt",sysEmployee.getString("empCard"));
        busiWrapper.eq("bt_ap_state",1);
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(busiWrapper);
        List<String> busiList=new ArrayList<>();
        businessTripAps.forEach(e->{
            busiList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime())));
        });
        for (int i = 0; i < busiList.size(); i++) {
            if(day.substring(0,10).equals(busiList.get(i))){
                System.out.println("出差日");
                flog=false;
                return 5;
            }
        }
        QueryWrapper<HolidayAp> wrapper5=new QueryWrapper<>();
        wrapper5.eq("user_ho",sysEmployee.getString("empCard"));
        wrapper5.eq("ho_state",1);
        List<HolidayAp> holidayAps = holidayApMapper.selectList(wrapper5);
        //请假
        List<String> list1=new ArrayList<>();
        holidayAps.forEach(e->{
            list1.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()), DateUtil.formatDate(e.getHoApEndTime())));
        });
        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i));
            if(day.substring(0,10).equals(list1.get(i))){
                System.out.println("今天请假了");
                flog=false;
                return 1;
            }
        }
        strings.removeAll(busiList);//去除出差
        strings.removeAll(betweenDate);//去除加班
        strings.removeAll(list1);//去除请假

        if(flog){
                QueryWrapper<Attendance> wrapper=new QueryWrapper<>();
                wrapper.like("at_time",day.substring(0,10));
                wrapper.eq("user_at",sysEmployee.getString("empCard"));
                Attendance attendance1 = attendanceMapper.selectOne(wrapper);
                //用户打卡时间
                Date dk = DateUtil.parse(day.substring(11, 19));
                System.out.println(day.substring(11,19));
                System.out.println("用户打卡时间："+dk);
                if(attendance1==null){
                    //当天没有打卡记录，进入新增
                    if(type==1){
                        //早上打卡时间
                        Date gdtDate=DateUtil.parse("09:00:00");
                        if(dk.getTime()<gdtDate.getTime()){
                            attendance.setAtStarRes("正常");
                        }else{
                            Late late=new Late();
                            late.setUserLate(sysEmployee.getString("empCard"));
                            late.setLateTime(dk);
                            //相差小时
                            long hour=DateUtil.between(gdtDate,dk, DateUnit.HOUR);
                            //相差分钟
                            long second=DateUtil.between(gdtDate,dk,DateUnit.MINUTE);
                            //如果迟到超过一小时
                            if(hour>=1){
                                long l = second - (hour * 60);
                                //新增严重迟到
                                late.setIsLate(1);
                                late.setLateResults(hour+"小时"+l+"分");
                                attendance.setAtStarRes("严重迟到");
                                lateMapper.insert(late);
                            } else if(hour==0 && second>0){
                                //新增普通迟到
                                late.setIsLate(0);
                                late.setLateResults(second+"分");
                                attendance.setAtStarRes("迟到");
                                lateMapper.insert(late);
                            }else{
                                //小于一分钟算正常
                                attendance.setAtStarRes("正常");
                            }
                        }
                        attendance.setAtStarTime(DateUtil.parse(day, "yyyy-MM-dd HH:mm:ss"));
                        attendanceMapper.insert(attendance);
                    }else{
                        Date gdtDate=DateUtil.parse("17:00:00");
                        if(dk.getTime()>gdtDate.getTime()){
                            attendance.setAtEndRes("正常");
                        }else{
                            Early early=new Early();
                            early.setUserEarly(sysEmployee.getString("empCard"));
                            early.setEarlyTime(dk);
                            //相差小时
                            long hour=DateUtil.between(gdtDate,dk,DateUnit.HOUR);
                            //相差分钟
                            long second=DateUtil.between(gdtDate,dk,DateUnit.MINUTE);
                            //如果早退超过一小时
                            if(hour>=1){
                                long l = second - (hour * 60);
                                //新增严重早退
                                early.setIsEarly(1);
                                early.setEarlyResults(hour+"小时"+(l+1)+"分");
                                attendance.setAtEndRes("严重早退");
                                earlyMapper.insert(early);
                            }
                            if(hour==0 && second>0){
                                //新增普通早退
                                early.setIsEarly(0);
                                early.setEarlyResults((second+1)+"分");
                                attendance.setAtEndRes("早退");
                                earlyMapper.insert(early);
                            }else{
                                attendance.setAtEndRes("正常");
                            }
                        }
                        attendance.setAtEndTime(DateUtil.parse(day, "yyyy-MM-dd HH:mm:ss"));
                        attendanceMapper.insert(attendance);
                    }
                }else{
                    //有打卡记录，根据type,修改
                    if(type==1){
                        Date gdtDate=DateUtil.parse("09:00:00");
                        if(dk.getTime()<gdtDate.getTime()){
                            attendance1.setAtStarRes("正常");
                        }else{
                            Late late=new Late();
                            late.setUserLate(sysEmployee.getString("empCard"));
                            late.setLateTime(dk);
                            //相差小时
                            long hour=DateUtil.between(gdtDate,dk,DateUnit.HOUR);
                            //相差分钟
                            long second=DateUtil.between(gdtDate,dk,DateUnit.MINUTE);
                            //如果迟到超过一小时
                            if(hour>=1){
                                long l = second - (hour * 60);
                                //新增严重迟到
                                late.setIsLate(1);
                                late.setLateResults(hour+"小时"+l+"分");
                                attendance1.setAtStarRes("严重迟到");
                            }
                            if(hour==0 && second>0){
                                //新增普通迟到
                                late.setIsLate(0);
                                late.setLateResults(second+"分");
                                attendance1.setAtStarRes("迟到");
                            }
                            //新增
                            lateMapper.insert(late);
                        }
                        attendance1.setAtStarTime(DateUtil.parse(day, "yyyy-MM-dd HH:mm:ss"));
                        attendanceMapper.updateById(attendance1);
                    }else{
                        Date gdtDate=DateUtil.parse("17:00:00");
                        if(dk.getTime()>gdtDate.getTime()){
                            attendance1.setAtEndRes("正常");
                        }else{
                            System.out.println("早退啦!");
                            Early early=new Early();
                            early.setUserEarly(sysEmployee.getString("empCard"));
                            early.setEarlyTime(dk);
                            //相差小时
                            long hour=DateUtil.between(gdtDate,dk,DateUnit.HOUR);
                            //相差分钟
                            long second=DateUtil.between(gdtDate,dk,DateUnit.MINUTE);
                            //如果早退超过一小时
                            if(hour>=1){
                                long l = second - (hour * 60);
                                //新增严重早退
                                early.setIsEarly(1);
                                early.setEarlyResults(hour+"小时"+(l+1)+"分");
                                attendance1.setAtEndRes("严重早退");
                            }
                            if(hour==0 && second>0){
                                //新增普通早退
                                early.setIsEarly(0);
                                early.setEarlyResults((second+1)+"分");
                                attendance1.setAtEndRes("早退");
                            }
                            System.out.println(early.toString());
                            earlyMapper.insert(early);
                        }
                        attendance1.setAtEndTime(DateUtil.parse(day, "yyyy-MM-dd HH:mm:ss"));
                        attendanceMapper.updateById(attendance1);
                    }
                }
                return 2;
            }
        return 3;

    }

    //查看有无打卡
    @Override
    public int select1(String day) {
        String user = stringRedisTemplate.opsForValue().get("user");
        JSONObject jsonObject = JSONObject.parseObject(user);
        JSONObject sysEmployee = JSONObject.parseObject(jsonObject.getString("sysEmployee"));
        System.out.println(sysEmployee.getString("empCard")+"：是否打卡："+day);
        QueryWrapper<Attendance> wrapper=new QueryWrapper<>();
        wrapper.like("at_time",day);
        wrapper.eq("user_at",sysEmployee.getString("empCard"));
        Attendance attendance = attendanceMapper.selectOne(wrapper);
        if(attendance!=null){
            if(attendance.getAtStarTime()==null){
                //有下班
                System.out.println("有下班");
                return 1;
            }else if(attendance.getAtEndTime()==null){
                //有上班
                System.out.println("有上班");
                return 2;
            }else{
                //今天已经全部打卡
                System.out.println("打完了！");
                return 4;
            }
        }
        //今天没有打卡
        System.out.println("没有一个！");
        return 3;
    }

    //原始考勤记录--签到记录，根据当前月and工号
    @Override
    public List query(OriginalVo originalVo) {
        List<Attendance> list = attendanceMapper.allAtten(originalVo);
        return list;
    }

    //公司考勤
    @Override
    public Map all(String day) {
        Map map=new HashMap();
        //迟到
        List<LateDept> list = lateMapper.byDeptLate("2023-01");
        //早退
        List<EarlyVo> earlyVos = earlyMapper.byDeptEarly("2023-01");
        //迟到集合
        List<LateVo> lateVos = lateMapper.mouthLateList("2023-01");
        //早退集合
        List<EarlyVo2> earlyVo2s = earlyMapper.mouthEarlyList("2023-01");
        map.put("late",list);
        map.put("ealy",earlyVos);
        map.put("lateList",lateVos);
        map.put("earlyList",earlyVo2s);
        return map;
    }

    //moth
    @Override
    public List<MouthVo> mothAtten(String typeName,String userNo, String time, int is) {
        if("本人".equals(typeName)){
            return test(new SysEmployee(), time, is);
        }else if("部门".equals(typeName)){
            List<SysEmployee> sysEmployees = sysEmployeeServiceDubbo.depidtEmp(Integer.parseInt(time));
            List<MouthVo> mouthVoList=new ArrayList<>();
            sysEmployees.forEach(e->{
                mouthVoList.addAll(test(e,time,is));
            });
            return mouthVoList;
        }else if("公司".equals(typeName)){
            List<SysEmployee> empis = sysEmployeeServiceDubbo.empis();
            List<MouthVo> mouthVoList=new ArrayList<>();
            empis.forEach(e->{
                System.out.println("emp："+e.getEmpCard());
                System.out.println("23456"+e.getEmployeePost().getPostId());
                mouthVoList.addAll(test(e,time,1));
            });
            return mouthVoList;
        }else if("null".equals(typeName)){
            return test(new SysEmployee(), time, is);
        }else if("234".equals(typeName)){
            //查询离职人list
            return test(new SysEmployee(),time,is);
        }
        return null;
    }

    @Override
    public List<MouthVo> test(SysEmployee sysEmployee, String time, int is) {
        //今年的工作日
        List<String> yearWorkDay = GetAntten.getYearWorkDay();
        List<MouthVo> mouthVoList=new ArrayList<>();
        //出差
        List<String> betweenDate = new ArrayList<>();
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectAll(sysEmployee.getEmpCard(), is);
        businessTripAps.forEach(e->{
            betweenDate.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime())));
        });
        //加班
        List<String> ovList=new ArrayList<>();
        List<String> ovjList=new ArrayList<>();
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectOv(sysEmployee.getEmpCard(), is);
        overtimeAps.forEach(e->{
            if(e.getOvertimeTypeId()==1){
                //周末加班
                ovList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()),DateUtil.formatDate(e.getOtEndTime())));
            }else{
                //节假日加班
                ovjList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()),DateUtil.formatDate(e.getOtEndTime())));
            }
        });
        //请假
        List<String> holiList=new ArrayList<>();
        List<String> moneyHoliList=new ArrayList<>();
        List<HolidayAp> holidayAps = holidayApMapper.selectHoli(sysEmployee.getEmpCard(), is);
        holidayAps.forEach(e->{
            if(e.getHoIsMoney()==1){
                //带薪
                moneyHoliList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()),DateUtil.formatDate(e.getHoApEndTime())));
            }else{
                //不带薪
                holiList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()),DateUtil.formatDate(e.getHoApEndTime())));
            }
        });
        //最近一个月
        String e=DateUtil.formatDate(DateUtil.lastMonth());
        MouthVo mouthVo=new MouthVo();
            mouthVo.setUserNo(sysEmployee.getEmpCard());
            mouthVo.setDept(sysEmployee.getDept().getDeptName());
            mouthVo.setUserName(sysEmployee.getEmpName());
            mouthVo.setDate(e.substring(0,7));
            mouthVo.setSalaryIsClear(sysEmployee.getSalaryIsClear());
            mouthVo.setDeptId(sysEmployee.getDeptId());
            mouthVo.setPostName(sysEmployee.getEmployeePost().getPost().getPostName());
            mouthVo.setPostId(sysEmployee.getEmployeePost().getPostId());
            mouthVo.setUserState(sysEmployee.getEmpState());
            AllVo allVo=new AllVo();
            //查询月的打卡集合
            QueryWrapper<Attendance> attendWrapper=new QueryWrapper<>();
            attendWrapper.eq("user_at",sysEmployee.getEmpCard());
            attendWrapper.like("at_time",e.substring(0,7));
            List<Attendance> list = attendanceMapper.selectList(attendWrapper);
            List<String> atList=new ArrayList();//本月打卡集合
            list.forEach(e2->{
                atList.add(DateUtil.formatDate(e2.getAtTime()));
            });
            List<String> busi=new ArrayList<>();
            betweenDate.forEach(e1->{
                if(e1.indexOf(e.substring(0,7))==0){
                    busi.add(e1);//本月的出差日集合
                }
            });

            //周末加班
            List<String> ov=new ArrayList<>();
            ovList.forEach(e2->{
                if(e2.indexOf(e.substring(0,7))==0){
                    ov.add(e2);//本月加班集合
                }
            });

            //节假日加班
            List<String> ovj=new ArrayList<>();
            ovjList.forEach(e2->{
                if(e2.indexOf(e.substring(0,7))==0){
                    ovj.add(e2);//本月加班集合
                }
            });

            List holi=new ArrayList();
            holiList.forEach(e3->{
                if(e3.indexOf(e.substring(0,7))==0){
                    holi.add(e3);//本月加班集合
                }
            });
            List moneyHoli=new ArrayList();
            moneyHoliList.forEach(e4->{
                if(e4.indexOf(e.substring(0,7))==0){
                    moneyHoli.add(e4);//本月加班集合
                }
            });
            //缺卡
            List<Attendance> qkList = attendanceMapper.attenMiss(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            //迟到
            List<Late> lateList = lateMapper.attenLate(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            //严重迟到
            List<Late> bigLateList = lateMapper.attenBigLate(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            //早退
            List<Early> earlies = earlyMapper.attenEarly(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            //严重早退
            List<Early> bigEarly = earlyMapper.attenBigEarly(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            //补卡
            List<ReplacementCardAp> rcList = replacementCardApMapper.reCard(sysEmployee.getEmpCard(), is, e.substring(0, 7));
            List<String> stringList = GetAntten.get(e.substring(0, 7));//调休集合
            List<String> stringList1 = GetAntten.queryMonthDay(e, stringList);//工作日+调休
            allVo.setYcq(stringList1.size());
            allVo.setSjcq(atList.size()+GetAntten.intersectList2(yearWorkDay,busi).size());
            allVo.setBusi(GetAntten.intersectList2(yearWorkDay,busi).size());
            allVo.setOv(ov.size());
            allVo.setOvj(ovj.size());
            if(allVo.getSjcq()>allVo.getYcq()){
                allVo.setKg(0);
            }else{
                allVo.setKg(allVo.getYcq()-allVo.getSjcq()-GetAntten.intersectList2(yearWorkDay,holi).size()-GetAntten.intersectList2(yearWorkDay,moneyHoli).size());
            }
            allVo.setQk(qkList.size());
            allVo.setLate(lateList.size());
            allVo.setBiglate(bigLateList.size());
            allVo.setEarly(earlies.size());
            allVo.setBigearly(bigEarly.size());
            allVo.setHoli(GetAntten.intersectList2(yearWorkDay,holi).size());
            allVo.setHoliMoney(GetAntten.intersectList2(yearWorkDay,moneyHoli).size());
            allVo.setRc(rcList.size());
            mouthVo.setAllVo(allVo);
            mouthVoList.add(mouthVo);
            atList.clear();
            busi.clear();
            ov.clear();;
            holi.clear();

        return mouthVoList;
    }

    //每日统计
    @Override
    public List<OneDateVo> oneDate(int deptId, String data) {
        List<OneDateVo> oneDateVos=new ArrayList<>() ;
        String today = DateUtil.today();
        List<String> todayList=new ArrayList<>();
        todayList.add(today);
        //今年工作日
        List<String> yearWorkDay = GetAntten.getYearWorkDay();
        boolean b=true;
        if(GetAntten.intersectList2(yearWorkDay,todayList).size()==0){
            b=false;
            //不是工作日
            //判断今天是否有人加班
            List<OvertimeAp> overtimeAps = overtimeApMapper.selectOneDate(today);
            if(overtimeAps.size()>0){
                overtimeAps.forEach(e->{
                    if("部门".equals(data)){
                        if(e.getSysEmployee().getDeptId()==deptId){
                            OneDateVo oneDateVo=new OneDateVo();
                            oneDateVo.setOv(true);
                            oneDateVo.setName(e.getSysEmployee().getEmpName());
                            oneDateVo.setDeptName(e.getSysEmployee().getDept().getDeptName());
                            oneDateVo.setUserCard(e.getUserOt());
                            oneDateVos.add(oneDateVo);
                        }
                    }else if("公司".equals(data)){
                        OneDateVo oneDateVo=new OneDateVo();
                        oneDateVo.setOv(true);
                        oneDateVo.setName(e.getSysEmployee().getEmpName());
                        oneDateVo.setDeptName(e.getSysEmployee().getDept().getDeptName());
                        oneDateVo.setUserCard(e.getUserOt());
                        oneDateVos.add(oneDateVo);
                    }
                });
                return oneDateVos;
            }else{
                return null;
            }
        }
        if(b){
            if(data.equals("部门")){
                List<SysEmployee> sysEmployees = sysEmployeeServiceDubbo.depidtEmp(deptId);
                //查考勤表
                sysEmployees.forEach(e->{
                    OneDateVo min = min(e,today);
                    oneDateVos.add(min);
                });
            }else if(data.equals("公司")){
                List<SysEmployee> sysEmployees = businessTripApMapper.selectEmp();
                sysEmployees.forEach(e->{
                    OneDateVo min = min(e, today);
                    oneDateVos .add(min);
                });
            }
        }
        return oneDateVos;
    }

    //person
    @Override
    public List<SysEmployee> person() {
        String user = stringRedisTemplate.opsForValue().get("user");
        SysUser sysUser = JSON.parseObject(user, SysUser.class);
        String roles = stringRedisTemplate.opsForValue().get("roles");
        List<SysUserRole> sysUserRoles = JSON.parseArray(roles, SysUserRole.class);
        boolean flog=false;
        for (int i = 0; i < sysUserRoles.size(); i++) {
            if(sysUserRoles.get(i).getSysRole().getRoleId()==1){
                flog=true;
            }else if(sysUserRoles.get(i).getRoleId()==4){
                flog=true;
            }
        }
        if(flog){
            return sysEmployeeServiceDubbo.selectEmp();
        }else{
            System.out.println(sysUser.getSysEmployee().getEmployeePost().getPost().getPostUp()==1);
            if(sysUser.getSysEmployee().getEmployeePost().getPost().getPostUp()==1){
                return sysEmployeeServiceDubbo.depidtEmp(sysUser.getSysEmployee().getDeptId());
            }
        }
        return null;
    }

    //每日统计min
    public OneDateVo min(SysEmployee e,String today){
        OneDateVo oneDateVo=new OneDateVo();
        oneDateVo.setName(e.getEmpName());
        oneDateVo.setDeptName(e.getDept().getDeptName());
        oneDateVo.setUserCard(e.getEmpCard());
        QueryWrapper<Attendance> attendanceQueryWrapper=new QueryWrapper<>();
        attendanceQueryWrapper.eq("user_at",e.getEmpCard());
        attendanceQueryWrapper.like("at_time",today);
        Attendance attendance = attendanceMapper.selectOne(attendanceQueryWrapper);
        if(attendance!=null){
            //当天有打卡的
            if(attendance.getAtStarTime()==null){
                //上班为空，缺卡
                oneDateVo.setUpQk(true);
            }else if(attendance.getAtStarRes().equals("迟到")||attendance.getAtStarRes().equals("严重迟到")){
                //上班迟到
                QueryWrapper<Late> lateQueryWrapper=new QueryWrapper<>();
                lateQueryWrapper.eq("user_late",e.getEmpCard());
                lateQueryWrapper.eq("late_time",attendance.getAtStarTime());
                Late late = lateMapper.selectOne(lateQueryWrapper);
                oneDateVo.setUpTime(new Timestamp(late.getLateTime().getTime()));
                oneDateVo.setUpRes("迟到"+late.getLateResults());
            }else{
                oneDateVo.setUpTime(new Timestamp(attendance.getAtStarTime().getTime()));
                oneDateVo.setUpRes(attendance.getAtStarRes());
            }

            if(attendance.getAtEndTime()==null){
                //下班缺卡
                oneDateVo.setDownQk(true);
            }else if(attendance.getAtEndRes().equals("早退")||attendance.getAtEndRes().equals("严重早退")){
                //下班早退
                QueryWrapper<Early> earlyQueryWrapper=new QueryWrapper<>();
                earlyQueryWrapper.eq("user_early",e.getEmpCard());
                earlyQueryWrapper.eq("early_time",attendance.getAtEndTime());
                Early early = earlyMapper.selectOne(earlyQueryWrapper);
                oneDateVo.setDownTime(new Timestamp(early.getEarlyTime().getTime()));
                oneDateVo.setDownRes("早退"+early.getEarlyResults());
            }else{
                oneDateVo.setDownTime(new Timestamp(attendance.getAtEndTime().getTime()));
                oneDateVo.setDownRes(attendance.getAtEndRes());
            }
        }else{
            //查看是否出差
            List<BusinessTripAp> businessTripAps = businessTripApMapper.selectBusiOneDate(today,e.getEmpCard());
            if(businessTripAps.size()>0){
                oneDateVo.setBusi(true);
            }else{
                //查看是否请假
                HolidayAp holidayAp = holidayApMapper.selectHoliOneDate(today, e.getEmpCard());
                if(holidayAp!=null){
                    oneDateVo.setHoli(true);
                }else{
                    oneDateVo.setKg(true);
                }
            }
        }
        return oneDateVo;
    }

}




